package com.ymatou.userservice.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.BindSinaWeiboUserRequestDtoBean;
import com.ymatou.userservice.parameter.SinaWeiBoUserRequestDtoBean;
import com.ymatou.userservice.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.userservice.service.BindSinaWeiboUserRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.AppTypeChannel;
import com.ymttest.utils.constant.ECode;
import com.ymttest.utils.constant.ThirdPartyType;
/**
*绑定微博账号接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BindSinaWeiboUserRequestDto{
	private static BindSinaWeiboUserRequestDtoBean bindsinaweibouserrequestdtoBean;
	private static BindSinaWeiboUserRequestDtoCall bindsinaweibouserrequestdtoCall;
	private static YmtUsersWapper ymtUser;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定微博账号接口");
		ymtUser = new YmtUsersWapper();
	}

	@Before
	public void caseUp() {
		bindsinaweibouserrequestdtoBean=new BindSinaWeiboUserRequestDtoBean();
		bindsinaweibouserrequestdtoCall=new BindSinaWeiboUserRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindsinaweibouserrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_001() {
		Logger.start(true,"微博账号未绑定过，绑定成功");
		try {
			String weiboUserId = "NewBindWeibo_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weiBoUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_SinaWeibo", "sWeiboUserId", weiboUserId);
			Logger.verifyIsNull(weiBoUser, "数据库中不存在该第三方账号");
			
			Logger.comment("前置准备：新创建一个用户");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindWeibo(token, "mainsite", weiboUserId);
			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
			
			Logger.comment("验证该账号不需要绑定手机");
			UserCallService.checkThirdPartyNeedBindMobile(weiboUserId, ThirdPartyType.WEIBO, ECode.USER_NO);
			Logger.comment("验证用该账号登陆成功");
			UserCallService.regByWeibo(weiboUserId, "", true, "登录成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_002() {
		Logger.start(true,"微博账号绑定过当前账号，已绑定过");
		try {
			String weiBoUserId = "NewBindWeibo_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weiBoUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_SinaWeibo", "sWeiboUserId", weiBoUserId);
			Logger.verifyIsNull(weiBoUser, "数据库中不存在该第三方账号");
			
			Logger.comment("前置准备：新创建一个用户");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindWeibo(token, "mainsite", weiBoUserId);	
			Logger.verifyEquals(ECode.SUCCESS, String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			
			Logger.comment("再次绑定");
			result = bindWeibo(token, "mainsite", weiBoUserId);
			Logger.verifyEquals("401", String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_003() {
		Logger.start(true,"微博账号已绑定过其他账号，绑定失败");
		try {
			String weiBoUserId = "NewBindWeibo_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weiBoUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_SinaWeibo", "sWeiboUserId", weiBoUserId);
			Logger.verifyIsNull(weiBoUser, "数据库中不存在该第三方账号");
			Logger.comment("用该账号注册一个新账号");
			UserCallService.regByWeibo(weiBoUserId, TestDataManager.getPhoneNum(), true, "注册成功");
			
			Logger.comment("在另一个码头账号里绑定该微博");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindWeibo(token, "mainsite", weiBoUserId);
			Logger.verifyEquals("401",  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_004() {
		Logger.start(true,"用户绑定了A微博账号->解绑A微博账号 ->用A登录失败 ->绑定A微博账号->绑定成功 ->用A登录成功");
		try {
			String weiBoUserId = "NewBindWeiboA_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weiBoUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_SinaWeibo", "sWeiboUserId", weiBoUserId);
			Logger.verifyIsNull(weiBoUser, "数据库中不存在该第三方账号");
			
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			int userId = (Integer)user.get("userId");
			
			Logger.comment("在码头账号里绑定该微博");
			Map result = bindWeibo(token, "mainsite", weiBoUserId);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByWeibo(weiBoUserId, "", true, "登录成功");
			
			Logger.comment("解绑A微博");
			UserCallService.unbindThirdParty(userId, weiBoUserId, ThirdPartyType.WEIBO);
			UserCallService.checkThirdPartyNeedBindMobile(weiBoUserId, ThirdPartyType.WEIBO, ECode.USER_YES);;
			
			Logger.comment("绑定A微博");
			result = bindWeibo(token, "mainsite", weiBoUserId);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByWeibo(weiBoUserId, "", true, "登录成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_005() {
		Logger.start(true,"用户绑定了A微博账号->解绑A微博账号->绑定B微博账号->绑定成功->用A登录失败 ->用B登录成功");
		try {
			String ali = "NewBindWeiboA_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map aliUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_SinaWeibo", "sWeiboUserId", ali);
			Logger.verifyIsNull(aliUser, "数据库中不存在该第三方账号");
			
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			int userId = (Integer)user.get("userId");
			
			Logger.comment("在码头账号里绑定该微博A");
			Map result = bindWeibo(token, "mainsite", ali);
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByWeibo(ali, "", true, "登录成功");
			
			Logger.comment("解绑A微博");
			UserCallService.unbindThirdParty(userId, ali, ThirdPartyType.WEIBO);
			UserCallService.checkThirdPartyNeedBindMobile(ali, ThirdPartyType.WEIBO, ECode.USER_YES);;
			
			Logger.comment("绑定B微博");
			String aliB = "NewBindWeiboB_"+TestDataManager.getRandomNum(10);
			result = bindWeibo(token, "mainsite", aliB);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByWeibo(aliB, "", true, "登录成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_006() {
		Logger.start(true,"未输入token，失败");
		try {
			String ali = "NewBindWeibo_"+TestDataManager.getRandomNum(10);
			Map result = bindWeibo(null, "mainsite", ali);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("错误的请求参数", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_007() {
		Logger.start(false,"未输入业务端类型，失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			String ali = "NewBindWeibo_"+TestDataManager.getRandomNum(10);
			Map result = bindWeibo(token, "", ali);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("错误的请求参数", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_008() {
		Logger.start(true,"未输入微博账号，失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			String ali = "NewBindWeibo_"+TestDataManager.getRandomNum(10);
			Map result = bindWeibo(token, "", ali);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("错误的请求参数", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_009() {
		Logger.start(true,"用户A绑定了A微博账号->解绑A微博账号->用户B绑定A微博账号->用户B解绑A微博账号");
		try {
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("注册码头账号");
			Map userA = UserCallService.registerUser(null, phone,null);
			String tokenA = userA.get("token").toString();
			int userAId = (Integer)userA.get("userId");
			
			Logger.comment("绑定了A微博账号");
			String weiboA = "unbindweibo_"+TestDataManager.getRandomNum(10);
			Map result = bindWeibo(tokenA, "mainsite", weiboA);
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			
			List<Map> thirdParty = new ArrayList<Map>();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.WEIBO, weiboA, true);
			UserCallService.checkThirdPartyBindYmt(userAId, thirdParty);
			
			Logger.comment("解绑A微博账号");
			result = UserCallService.unbindThirdParty(userAId, weiboA, ThirdPartyType.WEIBO);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.WEIBO, weiboA, false);
			UserCallService.checkThirdPartyBindYmt(userAId, thirdParty);
			
			Logger.comment("用户B绑定A微博账号");
			String phoneB = TestDataManager.getPhoneNum();
			Map userB = UserCallService.registerUser(null, phoneB,null);
			String tokenB = userB.get("token").toString();
			int userBId = (Integer)userB.get("userId");
			result = bindWeibo(tokenB, "mainsite", weiboA);
			
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.WEIBO, weiboA, true);
			UserCallService.checkThirdPartyBindYmt(userBId, thirdParty);
			
			Logger.comment("解绑A微博账号");
			result = UserCallService.unbindThirdParty(userBId, weiboA, ThirdPartyType.WEIBO);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.WEIBO, weiboA, false);
			UserCallService.checkThirdPartyBindYmt(userBId, thirdParty);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindSinaWeiboUserRequestDto_010() {
		Logger.start(true,"新Token格式微博账号未绑定过，绑定成功JIRACONN-2123");
		try {
			String weiboUserId = "NewBindWeibo_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weiBoUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_SinaWeibo", "sWeiboUserId", weiboUserId);
			Logger.verifyIsNull(weiBoUser, "数据库中不存在该第三方账号");
			
			Logger.comment("前置准备：新创建一个用户");
			UserRegisterInfoRequestDtoBean rgbmbean = UserCallService.initUserRegInfo();
			rgbmbean.setTokenSource("app");
			Map user=UserCallService.registerByMobile(rgbmbean);
			String token = user.get("accesstoken").toString();
			
			
			bindsinaweibouserrequestdtoBean=new BindSinaWeiboUserRequestDtoBean();
			bindsinaweibouserrequestdtoCall=new BindSinaWeiboUserRequestDtoCall();
			bindsinaweibouserrequestdtoBean.setToken(token);
			bindsinaweibouserrequestdtoBean.setClientType("app");
			SinaWeiBoUserRequestDtoBean wu = createWeiboUserBean(weiboUserId);
			wu.setTokenSource("app");
			bindsinaweibouserrequestdtoBean.setWeiBoUser(wu);
			bindsinaweibouserrequestdtoCall.setData(bindsinaweibouserrequestdtoBean);
			bindsinaweibouserrequestdtoCall.callService();
			
			Map result =bindsinaweibouserrequestdtoCall.getMap();
			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
			
			Logger.comment("验证该账号不需要绑定手机");
			UserCallService.checkThirdPartyNeedBindMobile(weiboUserId, ThirdPartyType.WEIBO, ECode.USER_NO);
			Logger.comment("验证用该账号登陆成功");
			UserCallService.regByWeibo(weiboUserId, "", true, "登录成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private SinaWeiBoUserRequestDtoBean createWeiboUserBean(String weiboUserId){
		SinaWeiBoUserRequestDtoBean regBean = new SinaWeiBoUserRequestDtoBean();
		regBean.setSWeiboUserId(weiboUserId);
		regBean.setSName("weiboTest_"+TestDataManager.getRandomNum(10));
		regBean.setSMail(TestDataManager.getEmail());
		regBean.setSMobile(TestDataManager.getPhoneNum());
		regBean.setClientIp("120.1.1.1");
		regBean.setDAddTime(YMTDateUtil.getBeforeOrNextDay(-1));
		regBean.setTerminalSource("ios");
		regBean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
//		regBean.setMobile(phone);
		regBean.setRegSource(1);
		return regBean;
	}

	private Map bindWeibo(String token, String clientType, String weiboUserId) throws ClientProtocolException, IOException, InterruptedException{
		Thread.sleep(1000);
		bindsinaweibouserrequestdtoBean=new BindSinaWeiboUserRequestDtoBean();
		bindsinaweibouserrequestdtoCall=new BindSinaWeiboUserRequestDtoCall();
		bindsinaweibouserrequestdtoBean.setToken(token);
		bindsinaweibouserrequestdtoBean.setClientType(clientType);
		bindsinaweibouserrequestdtoBean.setWeiBoUser(createWeiboUserBean(weiboUserId));
		bindsinaweibouserrequestdtoCall.setData(bindsinaweibouserrequestdtoBean);
		bindsinaweibouserrequestdtoCall.callService();
		return bindsinaweibouserrequestdtoCall.getMap();
	}
}